// @ts-nocheck
/**
 * @Date: 2022-04-17 17:40:41
 * @LastEditors: sailornpg
 * @LastEditTime: 2022-07-15 20:51:25
 * @FilePath: \weibo-api\tools.js
 * @Description: 工具函数
 */
const fse = require("fs-extra");
const fs = require("fs");
const path = require("path");
//分析@提到的用户
const analysis = (content, REG) => {
  const atUserIdList = [];
  content = content.replace(REG, (itemStr, nickName, atUserId) => {
    atUserIdList.push(atUserId);
    return itemStr;
  });
  return atUserIdList;
};
//删除博客是判断有没有图片，有的话就一起删除upload中的图片地址
const DelBlogWithImg = (imageUrlList) => {
  if (imageUrlList == null) {
    return;
  }
  if (typeof imageUrlList == "string") {
    imageUrlList = JSON.parse(imageUrlList);
  }
  let pathList = imageUrlList.map((image) => {
    let imgPath = image.split("/")[4];
    let filePath = path.join(__dirname, `/public/uploads/${imgPath}`);
    return filePath;
  });
  pathList.forEach(async (path) => {
    await fse.remove(`${path}`);
  });
};
module.exports = {
  analysis,
  DelBlogWithImg,
};
